package com.eduagent.xwqeduagent.config;

import com.eduagent.xwqeduagent.adviser.MyLoggerAdvisor;
import com.eduagent.xwqeduagent.chatmemory.RedisChatMemory;
import com.eduagent.xwqeduagent.tools.studenttools.CourseTools;
import lombok.RequiredArgsConstructor;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.client.advisor.api.Advisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@RequiredArgsConstructor
public class SpringAIConfig {


    private final ChatModel dashscopeChatModel;

    /**
     * 显式使用 dashscopeChatModel 构造 ChatClient
     */
    @Bean
    public ChatClient chatClient(Advisor loggerAdvisor,
                                 Advisor messageChatMemoryAdvisor) {
        // 直接通过 ChatClient.builder(ChatModel) 指定模型
        return ChatClient
                .builder(this.dashscopeChatModel)    // ← 指定 dashscopeChatModel :contentReference[oaicite:0]{index=0}
                .defaultAdvisors(loggerAdvisor, messageChatMemoryAdvisor) // 添加 Advisor
                .build();
    }

    /**
     * 日志记录器
     */
    @Bean
    public Advisor loggerAdvisor() {
        return new MyLoggerAdvisor();
    }

    @Bean
    public ChatMemory chatMemory() {
        return new RedisChatMemory();
    }

    /**
     * 基于Redis的会话记忆，聊天记忆整合到system message中实现多轮对话
     */
    @Bean
    public Advisor messageChatMemoryAdvisor(ChatMemory chatMemory) {
        return new MessageChatMemoryAdvisor(chatMemory);
    }
}
